package com.web.kdl.dao.maDataReport;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.web.kdl.entity.historyEvent.HistoryEvent;
import com.web.kdl.entity.maDataReport.DataReport;
import com.web.kdl.pojo.form.DataReportForm;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.mapping.ResultSetType;
import org.apache.ibatis.session.ResultHandler;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

@Mapper
@Repository
public interface DataReportDao extends BaseMapper<DataReport> {
//    @Select("select *" +
//            " FROM `${tableName}` " +
//            "")
    List<DataReport> selectAllDataReport(String tableName);
    List<DataReport> selectAllDataReportByTime(String tableName, LocalDateTime startTime, LocalDateTime endTime);
    
    /**
     * 游标分页查询（支持动态表名）
     */
    List<DataReport> selectPageByTime(
            @Param("tableName") String tableName,
            @Param("startTime") LocalDateTime startTime,
            @Param("endTime") LocalDateTime endTime,
            @Param("cursorTime") LocalDateTime cursorTime,
            @Param("id") String id,
            @Param("isNext") Boolean isNext,
            @Param("pageSize") Integer pageSize,
            @Param("deviceName") String deviceName
    );

    /**
     *   @Options(resultSetType = ResultSetType.FORWARD_ONLY, fetchSize = 1000)
     *   resultSetType = ResultSetType.FORWARD_ONLY：只能向前滚动，不能回退，节省内存
     *   fetchSize = 1000：每次从数据库取 1000 条到内存，而不是一次性全取
     */
    @Options(resultSetType = ResultSetType.FORWARD_ONLY, fetchSize = 1000)
    void streamSelectByTime(@Param("tableName") String tableName,
                            @Param("form") DataReportForm form,
                            @Param("handler") ResultHandler<DataReport> handler
                            );
    /**
     *   @Options(resultSetType = ResultSetType.FORWARD_ONLY, fetchSize = 1000)
     *   resultSetType = ResultSetType.FORWARD_ONLY：只能向前滚动，不能回退，节省内存
     *   fetchSize = 1000：每次从数据库取 1000 条到内存，而不是一次性全取
     */

    @Options(resultSetType = ResultSetType.FORWARD_ONLY, fetchSize = 1000)
    void streamSelectTableListByTime(@Param("tableName") List<String> tableList,
                            @Param("startTime") LocalDateTime startTime,
                            @Param("endTime") LocalDateTime endTime,
                            ResultHandler<HistoryEvent> handler);
}
